package com.Akronmcp.hta.obj;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;

import com.Akronmcp.hta.main.HM;

import me.clip.placeholderapi.PlaceholderAPI;

/**条件类*/
public class HCondition {

	
	String type;
	String args[];
	
	
	
	
	/**通过条件类型,参数集,构造条件对象*/
	public HCondition(String type,String args[])
	{
		this.type=type;
		this.args=args;
		
	}
	
	/**反向筛选*/
	public List<Player> obFilt(List<Player> pl)
	{
		List<Player> pl2 = new ArrayList<Player>();
		
		
		for(Player p:pl)
		{
			if(!Check(p).equals("yes"))
			pl2.add(p);
		}
		
		return pl2;
	}
	
	
	
	/**筛选玩家*/
	public List<Player> Filt(List<Player> pl)
	{
		List<Player> pl2 = new ArrayList<Player>();
		
		
		for(Player p:pl)
		{
			if(Check(p).equals("yes"))
			pl2.add(p);
		}
		
		return pl2;
	}
	
	/**判断玩家是否符合条件,返回不达成的条件名,达成则返回"yes"*/
	public String Check(Player p)
	{
		try {
			
			switch(type)
			{
			  //判断是否在世界和坐标距离内
			  case "world":
			  Location l = p.getLocation();
			  if(l.getWorld().getName().equals(args[0]))
			  {
				 
			     if(args.length==5)
				  {
					 Location l2 = new Location(l.getWorld(), 
							 Double.valueOf(args[1]),
							 Double.valueOf(args[2]), 
							 Double.valueOf(args[3]));
					 
					 if(l.distance(l2)>Double.valueOf(args[4]))
					 {return "world";}
					  
				  }
			     
			     return "yes";
			  }
			  return "world";
			  
			  //判断权限
			  case "perm":
			  if(p.hasPermission(args[0]))
			  return "yes";
			  return "perm";
			  
			  //判断金钱是否大于
			  case "money":
			  if(HM.isVault)
			  if(HM.econ.getBalance(p)>Double.valueOf(args[0]))
			  return "yes";
			  return "money";
			  
			  //判断等级是否大于
			  case "level":
			  if(p.getLevel()>Double.valueOf(args[0]))
			  return "yes";
			  return "level";
			  
			  //判断名字
			  case "name":
			  if(p.getName().equals(args[0]))
			  return "yes";
			  return "name";
				
			  //判断组
			  case "group":
			  if(HM.isVault)
			  if(HM.perm.playerInGroup(p,args[0]))
			  return "yes";
			  return "group";
			  
			  //判断变量是否大于或者是否相同
			  case "papi":
			  double d;
			  try
			  {
				    d = Double.valueOf(PlaceholderAPI.setPlaceholders(p, args[0]));
				    if(d< Double.valueOf(args[1]))
		    		{
		    	      return "yes";
		    		}
             
			  }catch (NumberFormatException e) {
				   if(PlaceholderAPI.setPlaceholders(p, args[0]).equals(args[1]))
				   {
					   return "yes";
				   }
			  }
			  
			  return "papi";

			  default:
		      return "no";
			}
			
		}catch(Exception e)
		{//出错则不成立
			e.printStackTrace();
			return "wrong";
		}
		
		
		
		
	}
	
	
}
